package com.comment.Controlleer;

import com.api.entity.CourseComment;
import com.api.util.ResponseDTO;
import com.comment.Mapper.CommentMapper;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/*
    @ClassName CommentController
    @Description TODO
    @Date 2021/9/4、10:47
*/
@RestController
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {

    @Resource
    private CommentMapper mapper;

    @RequestMapping("/getCourseCommentList/{courseId}/{pageNum}/{pageSize}")
    public ResponseDTO getCourseCommentList(@PathVariable("courseId")Integer courseId,
                                                    @PathVariable("pageNum")Integer pageNum,
                                                    @PathVariable("pageSize")Integer pageSize){

        return new ResponseDTO(mapper.getCourseAll(courseId),"",mapper.getAllComment(courseId,pageNum,pageSize));
    }

    @RequestMapping("saveCourseComment")
    public void saveCourseComment(CourseComment comment){
        mapper.addComment(comment);
        mapper.addCommentFavorite(comment);
    }

    @RequestMapping("cancelFavorite/{commentId}/{userId}")
    public void cancelFavorite(@PathVariable("commentId")Integer commentId,
                               @PathVariable("userId")Integer userId){
        mapper.updateComment(commentId,userId);
        Integer i = mapper.selectLikeCount(commentId) -1;
        mapper.updateLikeCount(i,commentId);
    }

    @RequestMapping("saveFavorite/{commentId}/{userId}")
    public void saveFavorite(@PathVariable("commentId")Integer commentId,
                               @PathVariable("userId")Integer userId){
        mapper.updateCommentSave(commentId,userId);
        Integer i = mapper.selectLikeCount(commentId)+1;
        mapper.updateLikeCount(i,commentId);
    }


}
